/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.andrea.girardello.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.TimerTask;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Image;
import net.andrea.girardello.view.CamView;

/**
 *
 * @author agirardello
 */
public class WebcamConnector extends TimerTask {

    CamView view;
    
    public WebcamConnector(CamView view) {
        this.view = view;
    }
    
    /**
     * TODO: resize the image accordingly to the size of display
     * @return
     * @throws java.io.IOException
     */
    private Image getImage() throws IOException {
        Thread t = new Thread();
        String url = "http://aws.unibz.it/mensawebcam/StreamImage.aspx";        
        HttpConnection httpConn = (HttpConnection)Connector.open(url);
        InputStream in = httpConn.openInputStream();
        return Image.createImage(in);
    }

    public void run() {
        try {
            view.setImage(getImage());
        } catch (IOException ex) {
            view.setImage( ImageLoader.getImage("warning.png") );
        }
    }
    
}
